package com.aramis.mydytt.ui.dialog

import android.app.Activity
import android.view.View
import android.view.ViewGroup
import android.widget.ScrollView
import android.widget.TextView
import com.aramis.mydytt.R
import com.aramis.mydytt.ext.logE
import com.aramis.mydytt.ui.bean.MovieDetailBean
import com.aramis.mydytt.ui.bean.MovieDetailNameBean
import java.lang.StringBuilder

/**
 * Description: DetailCommentDialog
 * 电影详情全部简介dialog
 * @author lizhidan
 * Created: 2021/5/8
 */
class DetailDescriptionDialog(activity: Activity) : BottomDialog(activity) {
    private var movieDetailBean: MovieDetailBean? = null
    private var errorLayout: ViewGroup? = null
    private var scrollView: ScrollView? = null
    private var movieNameView: TextView? = null

    private var textDirector: TextView? = null
    private var textDirectorContent: TextView? = null
    private var textActor: TextView? = null
    private var textActorContent: TextView? = null
    private var textDescription: TextView? = null
    private var textDescriptionContent: TextView? = null

    override fun getLayoutId(): Int = R.layout.dialog_detail_description

    override fun onInit() {
        super.onInit()
        movieNameView = findViewById(R.id.text_movie_name)
        errorLayout = mDecorView?.findViewById(R.id.layout_dialog_description_error)
        scrollView = mDecorView?.findViewById(R.id.scroll_content)
        mDecorView?.findViewById<View>(R.id.image_close)?.setOnClickListener {
            dismiss()
        }
        textDirector = findView(R.id.text_director)
        textDirectorContent = mDecorView?.findViewById(R.id.text_director_content)
        textActor = mDecorView?.findViewById(R.id.text_actor)
        textActorContent = mDecorView?.findViewById(R.id.text_actor_content)
        textActor = mDecorView?.findViewById(R.id.text_actor)
        textActorContent = mDecorView?.findViewById(R.id.text_actor_content)
        textDescription = mDecorView?.findViewById(R.id.text_description)
        textDescriptionContent = mDecorView?.findViewById(R.id.text_description_content)
        underCoverHeight(mDecorView?.findViewById(R.id.layout_dialog_description))
    }

    fun setData(bean: MovieDetailBean?) {
        this.movieDetailBean = bean
    }

    fun setMovieName(movieName: String?) {
        movieNameView?.text = movieName
    }

    override fun show() {
        refreshViews()
        super.show()
        logE("show")
    }

    private fun refreshViews() {
        if (movieDetailBean != null) {
            scrollView?.visibility = View.VISIBLE
            errorLayout?.visibility = View.INVISIBLE
            refreshContentViews(movieDetailBean!!)
        } else {
            scrollView?.visibility = View.INVISIBLE
            errorLayout?.visibility = View.VISIBLE
        }
    }

    private fun refreshContentViews(bean: MovieDetailBean) {
        setupActorLine(textDirector, textDirectorContent, bean.directors)
        setupActorLine(textActor, textActorContent, bean.actors)
        textDescriptionContent?.text = bean.description
    }

    private fun setupActorLine(titleView: TextView?, contentView: TextView?, list: List<MovieDetailNameBean>?) {
        var show = false
        if (list != null) {
            val stringBuild = StringBuilder()
            list.forEachIndexed { index, movieDetailDirectorsBean ->
                if (movieDetailDirectorsBean.name != null && movieDetailDirectorsBean.name.isNotBlank()) {
                    stringBuild.append(movieDetailDirectorsBean.name)
                    if (index < list.size - 1) {
                        stringBuild.append(",")
                    }
                }
            }
            if (stringBuild.isNotEmpty()) {
                show = true
                contentView?.text = stringBuild.toString()
            }
        }
        titleView?.visibility = if (show) View.VISIBLE else View.GONE
        contentView?.visibility = if (show) View.VISIBLE else View.GONE
    }
}